#! /bin/bash
#vamaker tag and tar 

# source version.sh
source ./scheme.sh

# allways 1
VERSION='1';
# current sprint
SPRINT='1';
# bug fix version
BUGFIX='0';
# build NO.
build=0;

# vamdsp_1.1.0.1_201309061358
LAST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1));

# get project name
project=${LAST_TAG:0:6};

# project version, allways 1 for now
version=${LAST_TAG:7:1};

# current sprint
# sprint=${LAST_TAG:11:1};
sprint="1";

# last debug version
debug=${LAST_TAG:11:1};

declare -i toBuild=${#LAST_TAG}-13-13
# last build
build=${LAST_TAG:13:${toBuild}};

declare -i new_build=${build}+1;
# tagdate=`date +%Y%m%d%H%M`;
TAG=${project}_${version}.${sprint}.${debug}.${new_build}_${tagdate}
TAR=${TAG}.tar.gz

# jobs
# tag 

`git tag ${TAG}`
`git push --tag`
`echo ${TAG} > web/ver.html`

if [[ $? -eq 0 ]]
then
    echo -e "${Gre}Tag is ready: ${TAG}${RCol}"
else
    exit -e "${Red}Tag git repository failed!${RCol}"
fi

# ##############
# pack 
# keep upload and runtime directories
# ###############
`tar -zc --exclude='./runtime' --exclude='.git/*' --exclude='pkey' --exclude='tags' --exclude='./composer.json' --exclude='./composer.lock' --exclude='./composer.phar' --exclude='./runtime/*' --exclude='LICENSE.md' --exclude='./yii.bat' --exclude='./node_modules' --exclude='./web/tools' -f ${TAR} ./*`

if [[ $? -eq 0 ]]
then
    echo -e "${Gre}Pack is ready: ${TAR}${RCol}"
else
    exit -e "${Red}Pack current repository failed!${RCol}"
fi


